﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Security.Cryptography;
using System.Web.UI.HtmlControls;

public partial class Admin_OrderManager : ASPXBaseClass
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.txtBeginDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            this.txtEndDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            BindGdv();
        }
    }

    /// <summary>
    /// 绑定订单
    /// </summary>
    void BindGdv()
    {
        DateTime BeginDate, EndDate;
        BeginDate = DateTime.Now;
        EndDate = BeginDate.AddDays(1).AddMilliseconds(-3);
        GZ.Entity.EOrder obj = new GZ.Entity.EOrder();
        if (this.txtOrderId.Text.Trim().Length > 0)
        {
            obj.OrderId = Convert.ToInt32(this.txtOrderId.Text.Trim());
        }
        if (this.txtCustomerName.Text.Trim().Length > 0)
        {
            obj.CustomerName = this.txtCustomerName.Text.Trim();
        }
        if (this.txtEmail.Text.Trim().Length > 0)
        {
            obj.Email = this.txtEmail.Text.Trim();
        }
        if (this.txtTelNum.Text.Trim().Length > 0)
        {
            obj.TelNum = this.txtTelNum.Text.Trim();
        }
        if (this.drpOrderState.SelectedValue != "0")
        {
            obj.OrderState = Convert.ToInt16(this.drpOrderState.SelectedValue);
        }
        if (this.drpOrderType.SelectedValue != "0")
        {
            obj.OrderType = Convert.ToInt32(this.drpOrderType.SelectedValue);
        }
        else
        {
            obj.OrderType = 0;
        }

        if (this.txtBeginDate.Text.Trim().Length > 0 && this.txtEndDate.Text.Trim().Length > 0)
        {
            BeginDate = Convert.ToDateTime(this.txtBeginDate.Text.Trim());
            EndDate = Convert.ToDateTime(this.txtEndDate.Text.Trim());
        }
        if (this.drpPaymentType.SelectedItem.Text != "请选择")
        {
            obj.PaymentType = Convert.ToInt32(this.drpPaymentType.SelectedValue);
        }
        else
        {
            obj.PaymentType = 0;
        }
        string DateType = "CreateDate";
        if (this.drpDate.SelectedItem.Text == "配送日期")
        {
            DateType = this.drpDate.SelectedValue;
        }
        this.AspNetPager1.RecordCount = obj.FindRowCount(obj, BeginDate, EndDate, DateType);
        this.rptOrderList.DataSource = obj.Find(obj, BeginDate, EndDate, (this.AspNetPager1.CurrentPageIndex - 1) * this.AspNetPager1.PageSize, this.AspNetPager1.PageSize, DateType);
        this.rptOrderList.DataBind();

    }

    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindGdv();
    }


    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindGdv();
    }

    /// <summary>
    /// Repeater行事件
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void rptOrderList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int OrderId = Convert.ToInt32(e.CommandArgument);
        if (e.CommandName == "Verify")
        {
            new GZ.Entity.EOrder().VerifyOrder(OrderId);
        }
        if (e.CommandName == "RePrint")
        {
            ScriptManager.RegisterClientScriptBlock(this.rptOrderList, this.GetType(), "print", "<script>window.open('RePrint.aspx?orderid=" + OrderId + "','EditProduct','height=400,width=400,top=270,left=600,toolbar=no,menubar=no,location=0,fullscreen=0,scrollbars=yes,resizable=no,location=no,status=no');</script>", false);
        }
        if (e.CommandName == "Print")
        {
            ScriptManager.RegisterClientScriptBlock(this.rptOrderList, this.GetType(), "print", "<script>window.open('PrintOrder.aspx?orderid=" + OrderId + "','EditProduct','height=400,width=400,top=270,left=600,toolbar=no,menubar=no,location=0,fullscreen=0,scrollbars=yes,resizable=no,location=no,status=no');</script>", false);
        }
        if (e.CommandName == "Cancel")
        {
            new GZ.Entity.EOrder().CancelOrder(OrderId);
        }
        if (e.CommandName == "Rejection")
        {
            new GZ.Entity.EOrder().RejectionOrder(OrderId);
        }
        BindGdv();
    }

    /// <summary>
    /// rpt行绑定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rptOrderList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            HiddenField HOrderState = e.Item.FindControl("HOrderState") as HiddenField;
            HiddenField HOrderId = e.Item.FindControl("HOrderId") as HiddenField;
            int OrderState = Convert.ToInt16(HOrderState.Value);
            Button btnVerify = e.Item.FindControl("btnVerify") as Button;
            Button btnPrint = e.Item.FindControl("btnPrint") as Button;
            Button btnCancel = e.Item.FindControl("btnCancel") as Button;
            Button btnRejection = e.Item.FindControl("btnRejection") as Button;
            Button btnRePrint = e.Item.FindControl("btnRePrint") as Button;
            Repeater rptOrderDetail = e.Item.FindControl("rptOrderDetail") as Repeater;
            Int64 OrderId = Convert.ToInt64(HOrderId.Value);
            HtmlTableRow tr = e.Item.FindControl("row") as HtmlTableRow;
            tr.Style.Add("background-color", "#ff9999");
            switch (OrderState)
            {
                case 1:
                    btnVerify.Visible = true;
                    btnCancel.Visible = true;
                    break;
                case 2:
                    btnPrint.Visible = true;
                    break;
                case 3:
                    btnRejection.Visible = true;
                    btnRePrint.Visible = true;
                    break;
            }
            if (OrderState == 4 || OrderState == 5 || OrderState == 6)
            {
                tr.Style.Add("background-color", "#799ae1");
            }
            if (OrderState == 3)
            {
                tr.Style.Add("background-color", "yellow");
            }
            rptOrderDetail.DataSource = new GZ.Entity.EOrderDetail().GetOrderDetail(OrderId);
            rptOrderDetail.DataBind();
        }
    }

    /// <summary>
    /// 查询结果导出CSV格式
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnExportCSV_Click(object sender, EventArgs e)
    {
        DateTime BeginDate, EndDate;
        BeginDate = DateTime.Now;
        EndDate = BeginDate.AddDays(1).AddMilliseconds(-3);
        GZ.Entity.EOrder obj = new GZ.Entity.EOrder();
        if (this.txtOrderId.Text.Trim().Length > 0)
        {
            obj.OrderId = Convert.ToInt32(this.txtOrderId.Text.Trim());
        }
        if (this.txtCustomerName.Text.Trim().Length > 0)
        {
            obj.CustomerName = this.txtCustomerName.Text.Trim();
        }
        if (this.txtEmail.Text.Trim().Length > 0)
        {
            obj.Email = this.txtEmail.Text.Trim();
        }
        if (this.txtTelNum.Text.Trim().Length > 0)
        {
            obj.TelNum = this.txtTelNum.Text.Trim();
        }
        if (this.drpOrderState.SelectedValue != "0")
        {
            obj.OrderState = Convert.ToInt16(this.drpOrderState.SelectedValue);
        }
        if (this.drpOrderType.SelectedValue != "0")
        {
            obj.OrderType = Convert.ToInt32(this.drpOrderType.SelectedValue);
        }
        else
        {
            obj.OrderType = 0;
        }

        if (this.drpPaymentType.SelectedItem.Text != "请选择")
        {
            obj.PaymentType = Convert.ToInt32(this.drpPaymentType.SelectedValue);
        }
        else
        {
            obj.PaymentType = 0;
        }
        if (this.txtBeginDate.Text.Trim().Length > 0 && this.txtEndDate.Text.Trim().Length > 0)
        {
            BeginDate = Convert.ToDateTime(this.txtBeginDate.Text.Trim());
            EndDate = Convert.ToDateTime(this.txtEndDate.Text.Trim());
        }
        string DateType = "CreateDate";
        if (this.drpDate.SelectedItem.Text == "配送日期")
        {
            DateType = this.drpDate.SelectedValue;
        }

        DataTable dt = obj.GetCSVData(obj, BeginDate, EndDate, DateType);
        string data = GZ.Common.DataExportHelper.ExportCSV(dt);
        string temp = string.Format("attachment;filename={0}", "ExportData.csv");
        Response.ClearHeaders();
        Response.AppendHeader("Content-disposition", temp);
        Response.Charset = "utf-8";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.Write(data);
        Response.End();
    }

    /// <summary>
    /// 获取订单状态
    /// </summary>
    /// <param name="OrderState">订单状态码</param>
    /// <returns>订单状态文字说明</returns>
    public string GetOrderState(int OrderState)
    {
        switch (OrderState)
        {
            case 1:
                return "未审核";
            case 2:
                return "已审核";
            case 3:
                return "已打印";
            case 4:
                return "已完成";
            case 5:
                return "已取消";
            case 6:
                return "已拒收";
            default:
                return "";
        }
    }

    /// <summary>
    /// 获取付款方式
    /// </summary>
    /// <param name="PaymentType">付款方式</param>
    /// <returns>付款方式</returns>
    public string GetPaymentType(int PaymentType)
    {
        switch (PaymentType)
        {
            case 1:
                return "货到付款";
            case 2:
                return "财付通";
            case 3:
                return "支付宝";
            default:
                return "货到付款";
        }
    }

    /// <summary>
    /// 获取付款方式
    /// </summary>
    /// <param name="PaymentType">付款方式</param>
    /// <param name="IsPay">是否付款</param>
    /// <returns>付款方式</returns>
    public string GetPaymentState(string PaymentType, string IsPay)
    {
        if (PaymentType != "1")
        {
            if (IsPay == "1")
            {
                return "支付成功";
            }
            else
            {
                return "未支付";
            }
        }
        else
        {
            return "";
        }
    }



    protected void Timer1_Tick(object sender, EventArgs e)
    {
        BindGdv();
    }
}